package com.Linngdu664.drglaserpointer.event;

import com.Linngdu664.drglaserpointer.item.ItemRegistry;
import com.Linngdu664.drglaserpointer.misc.SoundRegister;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;

public class SoundPlayEvent {
    boolean laserPointerInHand=false;
    @SubscribeEvent
    public void onPlayerTick(TickEvent.PlayerTickEvent event){
        if(event.phase== TickEvent.Phase.START){
            Player player = event.player;
            Level level = player.getLevel();
            if (!level.isClientSide) {
                ItemStack item = player.getItemInHand(InteractionHand.MAIN_HAND);
                if(item.is(ItemRegistry.LASER_POINTER.get())){
                    if(!laserPointerInHand){
                        laserPointerInHand= true;
                        level.playSound(null, player.getX(), player.getY(), player.getZ(), SoundRegister.LASER_ON.get(), SoundSource.PLAYERS, 1.0F, 1.0F / (level.getRandom().nextFloat() * 0.4F + 1.2F) + 0.5F);
                        System.out.println("on");
                    }
                }else if(laserPointerInHand){
                    laserPointerInHand= false;
                    level.playSound(null, player.getX(), player.getY(), player.getZ(), SoundRegister.LASER_OFF.get(), SoundSource.PLAYERS, 1.0F, 1.0F / (level.getRandom().nextFloat() * 0.4F + 1.2F) + 0.5F);
                    System.out.println("off");
                }
            }
        }
    }
}
